﻿/*+===================================================================
File:      Bullet.cpp

Summary:   Định nghĩa các phương thức của CBullet.
===================================================================+*/

#include "Bullet.h"
#include "Global.h"
#include "PlayState.h"
#include "Explosion.h"
#include <math.h>

using namespace std;

CBullet::CBullet(D3DXVECTOR2 pos, int id, int typeID, float angle)
	: CMoveableObject(pos, id, typeID)
{
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_R_BULLET);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_S1_BULLET);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_S2_BULLET);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_S3_BULLET);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_L_BULLET);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_F_BULLET);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_ENEMY_SMALL_WHITE_BULLET);
	CResourcesManager::GetInstance()->GetSprite(ID_SPRITE_BULLET_EXPLOSION);
	switch (typeID)
	{
		case ID_R_BULLET:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_R_BULLET);
			m_v.x = 450;
			m_v.y = 450;
			m_box.width = 10;
			m_box.height = 10;
			break;
		case ID_M_BULLET:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S1_BULLET);
			m_v.x = 430;
			m_v.y = 430;
			m_box.width = 10;
			m_box.height = 10;
			break;
		case ID_S_BULLET:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S1_BULLET);
			m_v.x = 400.0f;
			m_v.y = 400.0f;
			m_box.width = 10;
			m_box.height = 10;
			break;
		case ID_ENEMY_SMALL_WHITE_BULLET:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_ENEMY_SMALL_WHITE_BULLET);
			m_v.x = 220;
			m_v.y = 220;
			m_box.width = 10;
			m_box.height = 10;
			break;
		case ID_ENEMY_SMALL_RED_BULLET:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_ENEMY_SMALL_RED_BULLET);
			m_v.x = 220;
			m_v.y = 220;
			m_box.width = 10;
			m_box.height = 10;
			break;
		case ID_ENEMY_BIG_BULLET:
			m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S1_BULLET);
			m_v.x = 300;
			m_v.y = 300;
			m_box.width = 10;
			m_box.height = 10;
			break;
	}

	// Tính toán lại vị trí viên đạn theo hướng bắn.
	m_v.x *= cos(angle * PI / 180.0f);
	m_v.y *= sin(angle * PI / 180.0f);
}

CBullet::~CBullet()
{
	if (m_pContainerGun)
		delete m_pContainerGun;
}

void CBullet::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	D3DXVECTOR2 posInViewPort = CViewport::GetInstance()->TransformMatrix(m_pos);

	if (posInViewPort.x < 0 || posInViewPort.x > SCREEN_WIDTH || posInViewPort.y < 0 || posInViewPort.y > SCREEN_HEIGHT)
	{
		m_bIsLive = false;
		m_pContainerGun->ReloadBullet();
		return;
	}

	m_pos += m_v * timeFrame;

	m_pSprite->Update(timeFrame);
	UpdateBox(timeFrame);

	m_nCount = m_nCount + 1;

	// Hiệu ứng đạn S to ra.
	if (m_nTypeId == ID_S_BULLET)
	if (m_nCount == 15)
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S2_BULLET);
	else if (m_nCount == 45)
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S3_BULLET);
}

void CBullet::Draw()
{
	if (!m_bIsLive)
		return;

	D3DXVECTOR2 pos = CViewport::GetInstance()->TransformMatrix(m_pos);

	// Đạn của bill
	if (200 <= m_nTypeId && m_nTypeId <= 204)
	{
		if (m_nCount % 2)
		{
			m_pSprite->Draw(D3DXVECTOR2((INT)pos.x, (INT)pos.y));
		}
	}
	else
	{
		m_pSprite->Draw(D3DXVECTOR2((INT)pos.x, (INT)pos.y));
	}
}

void CBullet::SetGun(CGun* gun)
{
	m_pContainerGun = gun;
}

bool CBullet::Kill()
{
	m_bIsLive = false;
	m_pContainerGun->ReloadBullet();

	// Đạn của bill mới có hiệu ứng nổ
	if (200 <= m_nTypeId && m_nTypeId <= 204)
	{
		CExplosion* explosion = new CExplosion(m_pos, 0, ID_BULLET_EXPLOSION);
		CPlayState::s_objsOutSideQuadTree.push_back(explosion);
	}

	return true;
}

